import Vue from 'vue'
import Router from 'vue-router'

// 动态路由统一导入
import approvalRouter from './modules/approval'
import attendanceRouter from './modules/attendance'
import departmentRouter from './modules/department'
import employeeRouter from './modules/employee'
import permissionRouter from './modules/permission'
import roleRouter from './modules/role'
import salaryRouter from './modules/salary'
import socialRouter from './modules/social'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *路由配置规则
 * hidden: true                   这个路由是否现在侧边栏菜单，默认false显示；；；（重要）
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           路由重定向
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    用户角色
    title: 'title'              菜单文字（重要）
    icon: 'svg-name'/'el-icon-x' 菜单图标，使用el-icon-xx或写svg目录下面的图标名称（重要）
    breadcrumb: false           设置为false不会显示在面包屑中
    activeMenu: '/example/list'  如果设置，将一直高亮
  }
 */
// 如果一个菜单有二级，但是二级只有一个 或者 二级多个但是只有一个显示(hidden为false)
// 那么就会把这个唯一的一个或者唯一显示的提升为一级菜单
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },
  approvalRouter,
  attendanceRouter,
  departmentRouter,
  employeeRouter,
  permissionRouter,
  roleRouter,
  salaryRouter,
  socialRouter,
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
